/*lic*/
#ifndef __BSP_FLASH_MODULE_H__
#define __BSP_FLASH_MODULE_H__

#include "main.h"

#include "SpiInitHelper.h"

// Flash控制单元
typedef struct __Tag_Flash_Ctrl_Def {
	char name[32];
	
	// 多个flash时候具体哪个flash进行读写
	uint8_t flash_status;
	
	// flash设备锁
	struct rt_mutex flash_lock;
	// spi控制对象
	Spi_Transport_Def spi_ch_obj;
	
	rt_err_t (*Flash_Write_Enable)(struct __Tag_Flash_Ctrl_Def* obj);
	rt_err_t (*Flash_Read_DMA)(struct __Tag_Flash_Ctrl_Def* obj, uint64_t address, void* data_buf, size_t read_len);
	rt_err_t (*Flash_Write_DMA)(struct __Tag_Flash_Ctrl_Def* obj, uint64_t address, void* data_buf, size_t write_len);
	rt_err_t (*Flash_Erase_4K)(struct __Tag_Flash_Ctrl_Def* obj, uint64_t address);
	rt_err_t (*Flash_Erase_Chip)(struct __Tag_Flash_Ctrl_Def* obj);
	// 按1K擦除芯片。这个命令不是所有芯片都有。
	rt_err_t (*Flash_Erase_1K)(struct __Tag_Flash_Ctrl_Def* obj, uint64_t address);
}Flash_Ctrl_Def;

extern Flash_Ctrl_Def flash_1_ctrl;

// w25q 取消写保护
rt_err_t (*Flash_Write_Enable)(struct __Tag_Flash_Ctrl_Def* obj);
// w25q DMA读取函数
rt_err_t W25q_Read_Dma_Interface(Flash_Ctrl_Def* obj, uint64_t address, void* data_buf, size_t read_len);
// w25q DMA写入函数函数
rt_err_t W25q_Write_Dma_Interface(Flash_Ctrl_Def* obj, uint64_t address, void* data_buf, size_t write_len);
// w25q DMA页擦除函数
rt_err_t W25q_Erase_4K_Interface(Flash_Ctrl_Def* obj, uint64_t address);
// w25q DMA芯片擦除函数
rt_err_t W25q_Erase_Chip(Flash_Ctrl_Def* obj);

// 初始化Flash
void Init_Flash_Ctrl_Obj(Flash_Ctrl_Def* obj, char* name, CM_SPI_TypeDef *SPI_CMx, CM_DMA_TypeDef *CM_DMAx_TX, CM_DMA_TypeDef *CM_DMAx_RX, uint8_t DMAx_TX_CH, uint8_t DMAx_RX_CH);

// DMA1 CH6 传输完成中断
// SPI1 FLASH DMA
void INT_SRC_DMA1_TC6_IrqCallback(void);

#endif /* __BSP_FLASH_MODULE_H__ */

/*eof*/
